<?php
/**
 * Created by PhpStorm.
 * User: front
 * Date: 2018/8/3
 * Time: 11:37
 */
namespace App\Services;

class Log
{
    static $file;

    /**
     * 记录日志
     * @param string $file 文件名
     * @param string $fun  调用函数
     * @param mixed $data  对应的数据
     * @param string $msg  描述信息
     */
    static function info($file = '', $fun = '', $data = [], $msg = '')
    {
        self::$file = base_path('storage/logs/log-info.log');
        $msg = (string) $msg;
        if (is_array($data) || is_object($data)) {
            $data = json_encode($data, JSON_UNESCAPED_UNICODE);
        }
        $dataType = gettype($data);
        if (!is_string($data)) {
            $data = '';
        }

        if ($file === '') {
            $file = __FILE__;
        }

        if ($fun === '') {
            $fun = 'info';
        }

        $msg = '[' . date('Y-m-d H:i:s') . ']  文件：' . $file . ' 函数：' . $fun . ' data类型:' . $dataType . ' ' . $msg . '  ' . $data . "\r\n";
        file_put_contents(self::$file, $msg, FILE_APPEND);
    }


    /*
     * 写日志
     * */
    public static function do_log($content, $path='', $filename='')
    {
        $path = empty($path) ? storage_path('logs') : $path;
        $log_file = $path.'/'.(empty($filename) ? date('Ymd').'.log' : $filename);

        // 创建目录
        if (!is_dir($path)) {
            $res = mkdir(iconv("UTF-8", "GBK", $path), 0777, true);
            if (!$res)
                return ['status' => 'error', 'message_code'=>10139, 'message' => '目录不存在'];
        }

        file_put_contents($log_file, $content, FILE_APPEND);
    }

}